﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.TranslateApprove.Model;
using Yipusoft.TranslateApprove.BLL;

public partial class System_ApproveMembers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //部门
            UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
            if (userInfo.DepartmentID.Substring(0, 4) == "1300")
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }
            else
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }

            //if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200510"))
            //{
            //    DepartmentList.SelectedValue = department.DepartmentID.Substring(0, 4) + "000";
            //    DepartmentList.Enabled = false;
            //    DepartmentListBind();
            //}
            //else

            //    if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200503"))
            //    {
            //        DepartmentList.SelectedValue = department.DepartmentID.Substring(0, 4) + "000";
            //        DepartmentList.Enabled = false;
            //        DepartmentListBind();
            //        UnitList.SelectedValue = department.DepartmentID;
            //        UnitList.Enabled = false;
            //    }
            BindUser();
            BindMember();
        }
    }


    void DepartmentListBind()
    {
        if (DepartmentList.SelectedValue == "")
        {
            UnitList.Items.Clear();

            UnitList.DataSource = null;
            UnitList.DataBind();
        }
        else
        {
            UnitList.Items.Clear();

            string strWhere = "(departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%')";
            UnitList.DataSource = Department.GetList(strWhere);
            UnitList.DataBind();
        }

    }

    void BindUser()
    {
        string where = "";
        if (UnitList.SelectedValue != "")
        {
            where = " departmentid='" + UnitList.SelectedValue + "' ";
        }
        else
        {
            where = " (departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%' ";
        }
        ddlMembers.DataSource = Yipusoft.Account.BLL.User.GetList(where);
        ddlMembers.DataBind();

    }
    protected void DepartmentList_SelectedIndexChanged(object sender, EventArgs e)
    {
        DepartmentListBind();
        BindUser();
    }
    protected void UnitList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindUser();
    }

    public string GetDepartmentName(string departmentID)
    {
        return Department.GetDepartment(departmentID).FullName;
    }

    void BindMember()
    {
        string where = "";
        int id = Convert.ToInt32(Request.QueryString["id"]);
        List<MemberInfo> list = Members.GetList(id);
        if (list.Count > 0)
        {
            for (int i = 0; i < list.Count; i++)
            {
                where += "'" + list[i].UserID + "',";
            }
            where = " UserID in(" + where.Substring(0, where.Length - 1) + ") ";
        }
        else
        {
            where = "1>1";
        }
        UserGrid.DataSource = Yipusoft.Account.BLL.User.GetList(where);
        UserGrid.DataBind();
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = ((e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void lkbAdd_Click(object sender, EventArgs e)
    {
        if (!Members.isExist(Convert.ToInt32(Request.QueryString["id"]), ddlMembers.SelectedValue))
        {
            MemberInfo info = new MemberInfo();
            info.DID = Convert.ToInt32(Request.QueryString["id"]);
            info.UserID = ddlMembers.SelectedValue;
            info.MName = ddlMembers.SelectedItem.Text;
            if (Members.Insert(info) != -1)
            {
                BindMember();
                Prompt.Text = "添加成功！";
            }
        }
        else
        {
            Prompt.Text = "用户已存在！";
        }
    }

    protected void UserGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "del")
        {
            if (Members.Delete(Convert.ToInt32(Request.QueryString["id"]), e.CommandArgument.ToString()))
            {
                BindMember();
                Prompt.Text = "删除成功！";
            }
        }
    }
}
